package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Topic;
import com.example.demo.entity.User;
import com.example.demo.mapper.TopicMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.TopicService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 动态 服务实现类
 *
 * @author 俊何
 * @since 2021-08-09
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

  @Autowired
  private UserMapper userMapper;
  @Autowired
  private TopicMapper topicMapper;

  @Override
  public List<Topic> topicListByUserName(String userName) {
    User user =
        new LambdaQueryChainWrapper<>(userMapper)
            .eq(User::getUsername, userName)
            .select(User::getId)
            .eq(User::getUsername, userName)
            .one();
    if (user == null) {
      return null;
    }
    return new LambdaQueryChainWrapper<>(topicMapper)
        .eq(Topic::getUserId, user.getId())
        .orderByDesc(Topic::getId)
        .list();
  }
}
